package utils

import (
	"fmt"
	"math"
	"strconv"
	"strings"
)

type LineStruct struct {
	Lat      float64
	Lon      float64
	Angle    float64
	AngleNum float64
}

func MergeSame(path []LineStruct) ([][]LineStruct, [][]LineStruct) {
	//path = path[2:]
	//fmt.Println("path", path)
	arr := []LineStruct{}
	idxStart := 0
	idxEnd := 0
	result := [][]LineStruct{}
	ptResult := [][]LineStruct{}
	numArr := []int{-1}

	for i, v := range path {
		if i > 0 && i < len(path)-1 {
			if len(arr) == 0 {
				arr = append(arr, path[i-1])
			}
			if len(arr) == 2 {
				if Tolerance(v.Angle, arr[0].Angle, 1) {
					idxEnd = i
				} else if !Tolerance(v.Angle, arr[0].Angle, 1) || i == len(path)-1 {
					if idxEnd-idxStart > 1 {
						numArr = append(numArr, idxStart, idxEnd)
						result = append(result, path[idxStart:idxEnd+1])
					}
					idxStart = i - 1

				}
				arr = arr[1:]
			}
			arr = append(arr, v)
		}
	}
	numArr = append(numArr, len(path))
	//fmt.Println("numArr", numArr)
	for i := 0; i < len(numArr); i++ {
		if i%2 == 0 {
			if numArr[i]+1 < numArr[i+1] {
				//fmt.Println(numArr[i]+1, numArr[i+1])
				r := path[numArr[i]+1 : numArr[i+1]]
				if len(r) > 0 {
					ptResult = append(ptResult, path[numArr[i]+1:numArr[i+1]])

				}
			}

		} else if i != len(numArr)-1 {
			ptResult = append(ptResult, []LineStruct{})
		}
	}
	//fmt.Println("ptResult", ptResult)
	return result, ptResult
}

// 为路径的拐角添加圆弧
func AddArc(path []string) []string {
	var result []string
	// 内切圆的半径
	r := 0.003
	path = RemoveDuplicates(path)
	fmt.Println(path)
	for idx, point := range path {
		if idx == len(path)-1 || idx == 0 {
			result = append(result, point)
			continue
		}
		pointArr := strings.Split(point, ",")
		lat, lon := StrToFloat64(pointArr[0]), StrToFloat64(pointArr[1])

		nextPointArr := strings.Split(path[idx+1], ",")
		nextLat, nextLon := StrToFloat64(nextPointArr[0]), StrToFloat64(nextPointArr[1])

		lastPointArr := strings.Split(path[idx-1], ",")
		lastLat, lastLon := StrToFloat64(lastPointArr[0]), StrToFloat64(lastPointArr[1])

		AB := math.Sqrt(math.Pow(lat-lastLat, 2) + math.Pow(lon-lastLon, 2))
		BC := math.Sqrt(math.Pow(nextLat-lat, 2) + math.Pow(nextLon-lon, 2))
		AC := math.Sqrt(math.Pow(nextLat-lastLat, 2) + math.Pow(nextLon-lastLon, 2))

		//计算AB和BC的夹角
		cosC := (math.Pow(AB, 2) + math.Pow(BC, 2) - math.Pow(AC, 2)) / (2 * AB * BC)
		angle := math.Acos(cosC)
		PB := r / math.Sin(angle/2)
		BMN := math.Sqrt(PB*PB - r*r)
		//fmt.Println(BMN)
		//判断BMN是否为NaN
		if math.IsNaN(BMN) {
			result = append(result, point)
			continue
		}

		//	计算A相对于B的角度
		ABAngle := math.Atan2(lat-lastLat, lon-lastLon)
		//	计算C相对于B的角度
		BCAngle := math.Atan2(nextLat-lat, nextLon-lon)

		Mlat := lat + BMN*math.Cos(ABAngle+math.Pi/2)
		Mlon := lon + BMN*math.Sin(ABAngle+math.Pi/2)

		Nlat := lat + BMN*math.Cos(BCAngle+math.Pi/2)
		Nlon := lon + BMN*math.Sin(BCAngle+math.Pi/2)

		fmt.Println("A", lastLat, lastLon)
		fmt.Println("B", lat, lon)
		fmt.Println("C", nextLat, nextLon)
		fmt.Println("M", Mlat, Mlon)
		fmt.Println("N", Nlat, Nlon)

		pointStrM := Float64ToStrFixed(Mlat, 3) + "," + Float64ToStrFixed(Mlon, 3)
		pointStrN := Float64ToStrFixed(Nlat, 3) + "," + Float64ToStrFixed(Nlon, 3)
		result = append(result, pointStrN)
		result = append(result, pointStrM)
	}
	return result
}

// 去除重复的项
func RemoveDuplicates(arr []string) []string {
	result := []string{}
	for _, v := range arr {
		if !Contains(result, v) {
			result = append(result, v)
		}
	}
	return result
}

// 判断切片中是否包含某个元素
func Contains(arr []string, str string) bool {
	for _, v := range arr {
		if v == str {
			return true
		}
	}
	return false
}

func Float64ToStrFixed(f float64, n int) string {
	//return strconv.FormatFloat(f, 'f', n, 64)
	ffff := math.Round(f*math.Pow10(n)) / math.Pow10(n)
	return strconv.FormatFloat(ffff, 'f', -1, 64)
}
